package com.three.crm.util;

import android.content.Context;

import androidx.annotation.StringRes;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String工具类
 * Created by lqd on 2022-06-15.
 */

public class TString
{
    private static final String ENC = "utf-8";

    /**
     * 判断字符串是否为空
     */
    public static boolean isEmpty(String str)
    {
        return (str == null || str.length() == 0 || str.equals("null") || "".equals(str));
    }

    public static <T> boolean notEmpty(List<T> list) {
        return !isEmpty(list);
    }

    public static <T> boolean isEmpty(List<T> list) {
        if (list == null || list.size() == 0) {
            return true;
        }
        return false;
    }


    /**
     * 中文编码
     */
    public static String encode(final String s)
    {
        try
        {
            return URLEncoder.encode(s, ENC);
        } catch(UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 精确2位
     */
    public static String formatDecimal(double p)
    {
        String str;
        try
        {
            str = new DecimalFormat("#0.00").format(p);
        } catch(Exception e)
        {
            e.printStackTrace();
            str = "0";
        }
        return str;
    }

    /**
     * 返回格式化字符串
     */
    public static String format(Context context, @StringRes int res, Object... args)
    {
        if(context == null || args == null)
        {
            return "";
        }
        try
        {
            return String.format(context.getString(res), args);
        } catch(Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 手机号验证
     */
    public static boolean isPhone(final String phone)
    {
        return !isEmpty(phone) && Pattern.matches("^[1][2345678][0-9]{9}$", phone);
    }

    /**
     * 密码验证
     */
    public static boolean isPassword(final String password)
    {
        return !isEmpty(password) && password.matches("^[a-zA-Z0-9_]{6,12}$");
    }

    /**
     * 格式化数字为千分位显示；
     * @param text；
     * @return
     */
    public static String fmtMicrometer(String text)
    {
        DecimalFormat df = null;
        if(text.indexOf(".") > 0)
        {
            if(text.length() - text.indexOf(".")-1 == 0)
            {
                df = new DecimalFormat("###,##0.");
            }else if(text.length() - text.indexOf(".")-1 == 1)
            {
                df = new DecimalFormat("###,##0.0");
            }else
            {
                df = new DecimalFormat("###,##0.00");
            }
        }else
        {
            df = new DecimalFormat("###,##0");
        }
        double number = 0.0;
        try {
            number = Double.parseDouble(text);
        } catch (Exception e) {
            number = 0.0;
        }
        return df.format(number);
    }

    public static boolean isNumericzidai(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 验证身份证号码
     */
    public static boolean personIdValidation(String text) {
        String regx = "[0-9]{17}[Xx]";
        String reg1 = "[0-9]{15}";
        String regex = "[0-9]{18}";
        return text.matches(regx) || text.matches(reg1) || text.matches(regex);
    }

    /**
     * 隐藏手机号中间四位
     *
     * @param mobile
     * @return
     */
    public static String HideMobile(String mobile) {
        StringBuilder sb = new StringBuilder();
        if (!isEmpty(mobile) && mobile.length() > 6) {

            for (int i = 0; i < mobile.length(); i++) {
                char c = mobile.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }

        }
        return sb.toString();
    }

    public static String hideMobile(String mobile) {
        if (!isEmpty(mobile) && isPhone(mobile)) {
            return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
        }
        return mobile;
    }

    public static String hideIdCard(String idCard) {
        if (!isEmpty(idCard) && personIdValidation(idCard)) {
            return idCard.replaceAll("(\\d{4})\\d{10}(\\d{4})","$1****$2");
        }
        return idCard;
    }

    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
    /**
     * 校验银行卡卡号
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardNo) {
        char bit = getBankCardCheckCode(cardNo
                .substring(0, cardNo.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardNo.charAt(cardNo.length() - 1) == bit;

    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId){
        if (nonCheckCodeCardId == null
                || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');

    }



}
